package objects;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javafx.scene.canvas.GraphicsContext;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class WavClip extends AudioClip {
	private Clip clip = null;
	private  AudioInputStream audio;
	
	public WavClip(String audioUrl, String id ,  String title, String uplaoder, String instrument , String genre ,int bpm , String key, float rating) {
		super(audioUrl, id , title , uplaoder, instrument , genre ,bpm ,key, rating);
		
	}

	@Override
	public void loadAudio(final String url){
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
                                    System.out.println("------URL " + url);
					URL aURL = new URL(url);
					audio = AudioSystem.getAudioInputStream(aURL);
					clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, audio.getFormat()));
					clip.open(audio);
					
					
					ready = true;
				} catch (MalformedURLException e) {
					
					e.printStackTrace();
				} catch (UnsupportedAudioFileException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (LineUnavailableException e) {
					e.printStackTrace();
				}
				
			}
		}).start();
	}
	@Override
	public void play(){
            if(clip==null){
                loadAudio();
            }
		if(ready){
			
			FloatControl gainControl = 
				    (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
			
			if(currentTrack!=null && currentTrack.getId()==0)
			gainControl.setValue(-10.0f); // Reduce volume by 10 decibels.
			
			
			clip.stop();
			clip.setMicrosecondPosition(0);
			clip.start();
		}
	}
	@Override
	public void stop() {
		clip.stop();
	}
	@Override
	public AudioClip clone(){
		AudioClip cloned = new WavClip(audioUrl, id, title , uplaoder, instrument , genre ,bpm ,key, rating);
		return cloned;
	}

	@Override
	public double getDuration() {
		AudioFormat format = audio.getFormat();
		long frames = audio.getFrameLength();
		double durationInSeconds = (frames+0.0) / format.getFrameRate();  
		return durationInSeconds;
	}

    @Override
    public void previewPlay() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void setVolume(double volume) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void setPan(double balance) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


	
}
